﻿/*
 * File: TwoDeeGamePlayBaseScene.cs
 * Author: Gourav Das
 * Purpose: Base Scene for 2D gameplay
 * Created: April 2011
 */

using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Threading;
using System.IO;
using _22yards._2D;
using _22yards.Scenes.Helpers;
using _22yards.Core;
using _22yards;
using _22yards.EnumDeclarations;
using _22yards.ThreeD;
using _22yards.Helpers;
using Komires.MataliPhysics;

namespace _22yards.Scenes
{
    /// <summary>
    /// This is a scene that implements the 2D Gameplay screen
    /// </summary>
    partial class TwoDeeGamePlayBaseScene : Scene
    {
        /// <summary>
        /// Constructor.
        /// </summary>
        public TwoDeeGamePlayBaseScene()
        {
            TransitioningTime = TimeSpan.FromSeconds(1.5);
            TransitionedTime = TimeSpan.FromSeconds(1.5);
            PhysxEngine = new PhysicsEngine("22Yards PhysxEngine");
            Gravity = new Vector3(0, -1f, 0);
        }

        void GraphicsDevice_DeviceReset(object sender, EventArgs e)
        {
            SetModelEffMaterialNTexture();
        }

        /// <summary>
        /// Updates the state of the game. This method checks the GameScreen.IsActive
        /// property, so the game will stop updating when the pause menu is active,
        /// or if you switch to a different application.
        /// </summary>
        public override void Update(GameTime _gameTime, bool _otherSceneHasFocus,
                                                       bool _coveredByOtherScene)
        {
            base.Update(_gameTime, _otherSceneHasFocus, false);

            // Gradually fade in or out depending on whether we are covered by the pause screen.
            if (_coveredByOtherScene)
                pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
            else
                pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);

            
            if (IsActive)
            {
                PhysxScene.Simulate(_gameTime.ElapsedGameTime.TotalSeconds);

                if (bMobileDissappear || bMobileAppear)
                    TransitionValue += (float)_gameTime.ElapsedGameTime.Milliseconds / 15f;

                AnimateInfoBarSlidingEffect();

                if (DblClickTimeBomb == true)
                {
                    DblClickElapsedTimer += _gameTime.ElapsedGameTime;
                }
                else
                {
                   DblClickElapsedTimer = TimeSpan.Zero;
                    //click1 = false;
                }

                photoFrames[0].Update();
                photoFrames[1].Update();
                photoFrames[2].Update();
                photoFrames[3].Update();

                rightSideBarModel.Update();
                Team1Logo.Update();
                Team2Logo.Update();

                if ((DisplayRemainingBatsMan || DisplayRemainingBowler) && FirstColCurrentXPos < FirstColFinalXPos)
                {
                    FirstColCurrentXPos += 2;
                    if (FirstColCurrentXPos > FirstColFinalXPos)
                        FirstColCurrentXPos = FirstColFinalXPos;
                }
                if ((DisplayRemainingBatsMan || DisplayRemainingBowler) && SecondColCurrentXPos < SecondColFinalXPos)
                {
                    SecondColCurrentXPos += 2;
                    if (SecondColCurrentXPos > SecondColFinalXPos)
                        SecondColCurrentXPos = SecondColFinalXPos;
                }
                if ((DisplayRemainingBatsMan || DisplayRemainingBowler) && ThirdColCurrentXPos < ThirdColFinalXPos)
                {
                    ThirdColCurrentXPos += 2;
                    if (ThirdColCurrentXPos > ThirdColFinalXPos)
                        ThirdColCurrentXPos = ThirdColFinalXPos;
                }

                //Change Infobar message every N secods
                elapsedTime += _gameTime.ElapsedGameTime;
                if (elapsedTime > TimeSpan.FromSeconds(INFOBARMESSAGEINTERVAL))
                {
                    idx++;
                    elapsedTime -= TimeSpan.FromSeconds(INFOBARMESSAGEINTERVAL);
                    sInfoBarMessage = Game22Yards.DynMsgStore.Messages[idx % Game22Yards.DynMsgStore.Size].Msg;
                }

                if (PhotoFrameAnimationTimeBomb)
                {
                    PhotoFramesAnimationElapsedTime += _gameTime.ElapsedGameTime;
                    if (PhotoFramesAnimationElapsedTime > TimeSpan.FromSeconds(1.2f))
                    {                       
                        if (PhotoFramesAnimationElapsedTime > TimeSpan.FromSeconds(1.7f))
                        {
                            PhotoFramesAnimationElapsedTime = TimeSpan.Zero;
                            if((photoFrames[0].IsMouseLeftClickSelected || photoFrames[1].IsMouseLeftClickSelected) && RendereredGroupToBatsmen)
                                DisplayRemainingBatsMan = true;
                            if ((photoFrames[2].IsMouseLeftClickSelected || photoFrames[3].IsMouseLeftClickSelected) && RendereredGroupToBatsmen)
                                DisplayRemainingBowler = true;
                            PhotoFrameAnimationTimeBomb = false;
                        }
                    }
                }

                if (TransitionTimeBomb)
                {
                    TransitionelapsedTime += _gameTime.ElapsedGameTime;
                    if (TransitionelapsedTime > TimeSpan.FromSeconds(1.2f))
                    {
                        if(!TextPrintedOnTexture)
                        {
                            if (SubScreenState == (short)TwoDEESubScreenState.Summary)
                            {
                                TextureSummaryScreen = TextureScreen;
                                DrawSummary(ref TextureSummaryScreen);
                                TextureMaterial tm = new TextureMaterial();
                                tm.ColorMapTexture = TextureSummaryScreen;
                                tm.NormalMapTexture = TextureScreenNormal;
                                tm.SpecularMapTexture = TextureScreenSpec;
                                rightSideBarModel.SetModelMeshTextureMaterial("screen", tm);
                                TextPrintedOnTexture = true;
                            }
                            if (SubScreenState == (short)TwoDEESubScreenState.ScoreCardView)
                            {
                                TextureScoreCardScreen = TextureScreen;
                                DrawScoreCardView(ref TextureScoreCardScreen);
                                TextureMaterial tm = new TextureMaterial();
                                tm.ColorMapTexture = TextureScoreCardScreen;
                                tm.NormalMapTexture = TextureScreenNormal;
                                tm.SpecularMapTexture = TextureScreenSpec;
                                rightSideBarModel.SetModelMeshTextureMaterial("screen", tm);
                                TextPrintedOnTexture = true;
                            }
                        }
                        if (TransitionelapsedTime > TimeSpan.FromSeconds(2.2f))
                        {
                            TransitionelapsedTime -= TimeSpan.FromSeconds(2.2f);
                            rightSideBarModel.transitionIn = true;
                            rightSideBarModel.Oscillate = true;
                            TransitionTimeBomb = false;
                        }
                    }
                }
            }
        }


        /// <summary>
        /// Lets the game respond to player input. Unlike the Update method,
        /// this will only be called when the gameplay scene is active.
        /// </summary>
        public override void HandleInput(InputEngine _input)
        {
            if (_input == null)
                throw new ArgumentNullException("null input");

            // Look up inputs for the active player profile.
            int playerIndex = (int)ControllingPlayer.Value;
            PlayerIndex plidx;

            if (_input.IsMouseLeftButtonPress(ControllingPlayer, out plidx))
            {
                DblClickTimeBomb = true;
                click1 = true;
            }

            KeyboardState keyboardState = _input.CurrentKeyboardStates[playerIndex];
            GamePadState gamePadState = _input.CurrentGamePadStates[playerIndex];
            MouseState mouseState = _input.CurrentMouseStates;

            KeyboardState lastkeyboardState = _input.LastKeyboardStates[playerIndex];

            // The game pauses either if the user presses the pause button, or if
            // they unplug the active gamepad. This requires us to keep track of
            // whether a gamepad was ever plugged in, because we don't want to pause
            // on PC if they are playing with a keyboard and have no gamepad at all!
            bool gamePadDisconnected = !gamePadState.IsConnected &&
                                       _input.GamePadWasConnected[playerIndex];

            if (_input.IsPauseGame(ControllingPlayer) || gamePadDisconnected)
            {
                sceneEngine.AddScene(new PauseScene(), ControllingPlayer);
            }
            else
            {

                if (keyboardState.IsKeyUp(Keys.X) && lastkeyboardState.IsKeyDown(Keys.X))
                {
                    KBRedBall = new CubeModel(content.Load<Model>(@"ThreeDEE/Models/KBRedBall/ball"), birdEyeCamera.Position, Vector3.Zero, new Vector3(1f, 1f, 1f), sceneEngine.GraphicsDevice, "KBRedBall", 1);

                    BallObject.InitLocalTransform.SetPosition(birdEyeCamera.Position);
                    BallObject.InitLocalTransform.SetLinearVelocity(new Vector3(0f, -1, 2));
                    BallObject.Integral.SetDensity(0.2f);
                    BallObject.Material.Restitution = 0.3f;
                    BallObject.Material.DynamicFriction = 0.4f;
                    BallObject.InitLocalTransform.SetAngularVelocity(0, 6f, 0);

                    KBRedBall.PhysicsObject = BallObject;
                    PhysxScene.UpdateFromInitLocalTransform(BallObject);                    
                }


                if (keyboardState.IsKeyDown(Keys.I))
                {
                    if (iRightSidebarStartheight > iRightSidebarEndheight && iInfoBarStartwidth < iInfobarEndWidth)
                        bStartSidebarShrinkingTransition = true;

                    if (iRightSidebarStartheight < iRightSidebarEndheight && iInfoBarStartwidth > iInfobarEndWidth)
                        bStartInfobarShrinkTransition = true;
                }

                if (keyboardState.IsKeyDown(Keys.F1))
                {
                    if (DisplayRemainingBatsMan || DisplayRemainingBowler)
                    {
                        DisplayRemainingBatsMan = false;
                        DisplayRemainingBowler = false;
                        FirstColCurrentXPos = -50;
                        SecondColCurrentXPos = -50;
                        ThirdColCurrentXPos = -50;

                        for (int i = 4; i < 22; i++)
                            photoFrames[i].Position.X = -50f;

                        FirstColCurrentXPos = -30;
                        SecondColCurrentXPos = -30;
                        ThirdColCurrentXPos = -30;
                        for (int i = 0; i < 4; i++)
                            photoFrames[i].IsMouseLeftClickSelected = false;

                        if (photoFrames[0].Oscillate == false)
                        {
                            photoFrames[0].SyncOscillationsWithModel(photoFrames[1]);
                            photoFrames[0].Oscillate = true;
                        }
                        if (photoFrames[1].Oscillate == false)
                        {
                            photoFrames[1].SyncOscillationsWithModel(photoFrames[0]);
                            photoFrames[1].Oscillate = true;
                        }
                        if (photoFrames[2].Oscillate == false)
                        {
                            photoFrames[2].SyncOscillationsWithModel(photoFrames[3]);
                            photoFrames[2].Oscillate = true;
                        }
                        if (photoFrames[3].Oscillate == false)
                        {
                            photoFrames[3].SyncOscillationsWithModel(photoFrames[2]);
                            photoFrames[3].Oscillate = true;
                        }

                        TransitionTimeBomb = true;
                        TransitionelapsedTime = TimeSpan.FromSeconds(1.2f);
                        if (SubScreenState != (short)TwoDEESubScreenState.Summary)
                        {
                            SubScreenState = (short)TwoDEESubScreenState.Summary;
                            TextPrintedOnTexture = false;
                        }
                    }

                    if (bShowField)
                    {
                        TransitionTimeBomb = true;
                        TransitionelapsedTime = TimeSpan.FromSeconds(1.2f);
                        if (SubScreenState != (short)TwoDEESubScreenState.Summary)
                        {
                            SubScreenState = (short)TwoDEESubScreenState.Summary;
                            TextPrintedOnTexture = false;
                        }
                        bMobileAppear = true;
                    }

                    if (SubScreenState != (short)TwoDEESubScreenState.Summary && (!rightSideBarModel.transitionOut && !rightSideBarModel.transitionIn))
                    {
                        if (!TransitionTimeBomb)
                        {
                            SubScreenState = (short)TwoDEESubScreenState.Summary;
                            rightSideBarModel.transitionOut = true;
                            rightSideBarModel.Oscillate = false;
                            TransitionTimeBomb = true;
                            TextPrintedOnTexture = false;
                        }
                    }
                }

                if (keyboardState.IsKeyDown(Keys.F2))
                {
                    if (DisplayRemainingBatsMan || DisplayRemainingBowler)
                    {
                        DisplayRemainingBatsMan = false;
                        DisplayRemainingBowler = false;
                        FirstColCurrentXPos = -50;
                        SecondColCurrentXPos = -50;
                        ThirdColCurrentXPos = -50;

                        for (int i = 4; i < 22; i++)
                            photoFrames[i].Position.X = -50f;

                        for (int i = 0; i < 4; i++)
                            photoFrames[i].IsMouseLeftClickSelected = false;

                        if (photoFrames[0].Oscillate == false)
                        {
                            photoFrames[0].SyncOscillationsWithModel(photoFrames[1]);
                            photoFrames[0].Oscillate = true;
                        }
                        if (photoFrames[1].Oscillate == false)
                        {
                            photoFrames[1].SyncOscillationsWithModel(photoFrames[0]);
                            photoFrames[1].Oscillate = true;
                        }
                        if (photoFrames[2].Oscillate == false)
                        {
                            photoFrames[2].SyncOscillationsWithModel(photoFrames[3]);
                            photoFrames[2].Oscillate = true;
                        }
                        if (photoFrames[3].Oscillate == false)
                        {
                            photoFrames[3].SyncOscillationsWithModel(photoFrames[2]);
                            photoFrames[3].Oscillate = true;
                        }

                        TransitionTimeBomb = true;
                        TransitionelapsedTime = TimeSpan.FromSeconds(1.2f);
                        if (SubScreenState != (short)TwoDEESubScreenState.ScoreCardView)
                        {
                            SubScreenState = (short)TwoDEESubScreenState.ScoreCardView;
                            TextPrintedOnTexture = false;
                        }
                    }

                    if (bShowField)
                    {
                        TransitionTimeBomb = true;
                        TransitionelapsedTime = TimeSpan.FromSeconds(1.2f);
                        if (SubScreenState != (short)TwoDEESubScreenState.ScoreCardView)
                        {
                            SubScreenState = (short)TwoDEESubScreenState.ScoreCardView;
                            TextPrintedOnTexture = false;
                        }
                        bMobileAppear = true;
                    }

                    if (SubScreenState != (short)TwoDEESubScreenState.ScoreCardView && (!rightSideBarModel.transitionOut && !rightSideBarModel.transitionIn))
                    {
                        if (!TransitionTimeBomb)
                        {
                            SubScreenState = (short)TwoDEESubScreenState.ScoreCardView;
                            rightSideBarModel.transitionOut = true;
                            rightSideBarModel.Oscillate = false;
                            TransitionTimeBomb = true;
                            TextPrintedOnTexture = false;
                        }
                    }
                }

                if (keyboardState.IsKeyDown(Keys.F3))
                {
                    if (DisplayRemainingBatsMan || DisplayRemainingBowler)
                    {
                        DisplayRemainingBatsMan = false;
                        DisplayRemainingBowler = false;
                        FirstColCurrentXPos = -50;
                        SecondColCurrentXPos = -50;
                        ThirdColCurrentXPos = -50;

                        for (int i = 4; i < 22; i++)
                            photoFrames[i].Position.X = -50f;

                        for (int i = 0; i < 4; i++)
                            photoFrames[i].IsMouseLeftClickSelected = false;

                        if (photoFrames[0].Oscillate == false)
                        {
                            photoFrames[0].SyncOscillationsWithModel(photoFrames[1]);
                            photoFrames[0].Oscillate = true;
                        }
                        if (photoFrames[1].Oscillate == false)
                        {
                            photoFrames[1].SyncOscillationsWithModel(photoFrames[0]);
                            photoFrames[1].Oscillate = true;
                        }
                        if (photoFrames[2].Oscillate == false)
                        {
                            photoFrames[2].SyncOscillationsWithModel(photoFrames[3]);
                            photoFrames[2].Oscillate = true;
                        }
                        if (photoFrames[3].Oscillate == false)
                        {
                            photoFrames[3].SyncOscillationsWithModel(photoFrames[2]);
                            photoFrames[3].Oscillate = true;
                        }

                        if (SubScreenState != (short)TwoDEESubScreenState.BirdEyeView)
                        {
                            SubScreenState = (short)TwoDEESubScreenState.BirdEyeView;
                        }
                    }

                    if (!bShowField)
                    {
                        bShowField = true;
                        bMobileDissappear = true;
                        SubScreenState = (short)TwoDEESubScreenState.BirdEyeView;
                    }
                }

                if (keyboardState.IsKeyDown(Keys.D1))
                {
                    bool exit = false;
                    for (int i = 0; i < photoFrames.Count; i++)
                    {
                        if (photoFrames[i].IsMouseLeftClickSelected)
                        {
                            exit = true;
                            break;
                        }
                    }

                    if (!exit)
                    {
                        photoFrames[0].IsMouseLeftClickSelected = true;
                    }
                }
                if (keyboardState.IsKeyDown(Keys.D2))
                {
                    bool exit = false;
                    for (int i = 0; i < photoFrames.Count; i++)
                    {
                        if (photoFrames[i].IsMouseLeftClickSelected)
                        {
                            exit = true;
                            break;
                        }
                    }

                    if (!exit)
                    {
                        photoFrames[1].IsMouseLeftClickSelected = true;
                    }                    
                }
                if (keyboardState.IsKeyDown(Keys.D3))
                {
                    bool exit = false;
                    for (int i = 0; i < photoFrames.Count; i++)
                    {
                        if (photoFrames[i].IsMouseLeftClickSelected)
                        {
                            exit = true;
                            break;
                        }
                    }

                    if (!exit)
                    {
                        photoFrames[2].IsMouseLeftClickSelected = true;
                    }
                }
                if (keyboardState.IsKeyDown(Keys.D4))
                {
                    bool exit = false;
                    for (int i = 0; i < photoFrames.Count; i++)
                    {
                        if (photoFrames[i].IsMouseLeftClickSelected)
                        {
                            exit = true;
                            break;
                        }
                    }

                    if (!exit)
                    {
                        photoFrames[3].IsMouseLeftClickSelected = true;
                    }
                }

                //If dbl click
                if (click1 && _input.IsMouseLeftButtonPress(ControllingPlayer, out plidx) && DblClickElapsedTimer > TimeSpan.FromMilliseconds(10) && DblClickElapsedTimer < TimeSpan.FromMilliseconds(DoubleClickDelay))
                {
                    DblClickTimeBomb = false;
                    click1 = false;
                    Vector3 nearsource = new Vector3((float)mouseState.X, (float)mouseState.Y, 0f);
                    Vector3 farsource = new Vector3((float)mouseState.X, (float)mouseState.Y, 49f);

                    Matrix world = Matrix.CreateTranslation(0, 0, 0);

                    Vector3 nearPoint = sceneEngine.GraphicsDevice.Viewport.Unproject(nearsource, staticCamera.ProjectionMatrix, staticCamera.ViewMatrix, world);

                    Vector3 farPoint = sceneEngine.GraphicsDevice.Viewport.Unproject(farsource, staticCamera.ProjectionMatrix, staticCamera.ViewMatrix, world);
                    Vector3 direction = farPoint - nearPoint;
                    direction.Normalize();
                    Ray pickRay = new Ray(nearPoint, direction);
                    bool exit = false;
                    for (int i = 0; i < photoFrames.Count; i++)
                    {
                        if (pickRay.Intersects(photoFrames[i].BoundingSphere) != null)
                        {
                            for (int j = 0; j < photoFrames.Count; j++)
                            {
                                if (photoFrames[j].IsMouseLeftDoubleClickSelected)
                                {
                                    exit = true;
                                    break;
                                }
                            }

                            if (exit)
                                break;

                            photoFrames[i].IsMouseLeftDoubleClickSelected = true;
                            break;
                        }
                        
                     }
                }

                //single click
                else if (click1 && DblClickElapsedTimer >= TimeSpan.FromMilliseconds(DoubleClickDelay))
                {
                    DblClickTimeBomb = false;
                    click1 = false;
                    Vector3 nearsource = new Vector3((float)mouseState.X, (float)mouseState.Y, 0f);
                    Vector3 farsource = new Vector3((float)mouseState.X, (float)mouseState.Y, 1f);

                    Matrix world = Matrix.CreateTranslation(0, 0, 0);

                    Vector3 nearPoint = sceneEngine.GraphicsDevice.Viewport.Unproject(nearsource, staticCamera.ProjectionMatrix, staticCamera.ViewMatrix, world);

                    Vector3 farPoint = sceneEngine.GraphicsDevice.Viewport.Unproject(farsource, staticCamera.ProjectionMatrix, staticCamera.ViewMatrix, world);
                    Vector3 direction = farPoint - nearPoint;
                    direction.Normalize();
                    Ray pickRay = new Ray(nearPoint, direction);
                    bool exit = false;
                    for (int i = 0; i < photoFrames.Count; i++)
                    {
                        if (pickRay.Intersects(photoFrames[i].BoundingSphere) != null)
                        {
                            for (int j = 0; j < photoFrames.Count; j++)
                            {
                                if (photoFrames[j].IsMouseLeftClickSelected)
                                {
                                    exit = true;
                                    break;
                                }
                            }

                            if (exit)
                                break;

                            photoFrames[i].IsMouseLeftClickSelected = true;
                            break;
                        }

                    }
                }


            }
        }

        /// <summary>
        /// Draws the gameplay scene.
        /// </summary>
        public override void Draw(GameTime _gameTime)
        {
            sceneEngine.GraphicsDevice.SetRenderTarget(RenderTarget1);
            RenderTarget1.GraphicsDevice.Clear(Color.DarkSlateBlue);

            DrawBase2DScene();
            DrawDefaultViewPortItems();

            sceneEngine.GraphicsDevice.SetRenderTarget(null);
            OriginalViewPortTexture = RenderTarget1;

            //DrawRightViewPortItems
            sceneEngine.GraphicsDevice.Viewport = RtViewPort;

            if (bShowMobile || bMobileAppear || bMobileDissappear)
            {
                ///////////////////////////////////////
                //Scene1
                //////////////////////////////////////
                sceneEngine.GraphicsDevice.SetRenderTarget(RenderTarget2);
                RenderTarget2.GraphicsDevice.Clear(Color.DarkSlateBlue);
                sceneEngine.SpriteBatch.Begin();
                {
                    sceneEngine.SpriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, RtViewPort.Width, RtViewPort.Height), new Rectangle(RtViewPort.X, RtViewPort.Y, RtViewPort.Width, RtViewPort.Height), Color.White);
                }
                sceneEngine.SpriteBatch.End();

                rightSideBarModel.Draw(staticCamera, Matrix.Identity);
                DisplayRemainingPhotoFrames();
                sceneEngine.GraphicsDevice.SetRenderTarget(null);
                RtViewPortTexture1 = RenderTarget2;
            }

            if (bShowField || bMobileAppear || bMobileDissappear)
            {
                //////////////////////////////////////////////
                //// SCENE 2
                /////////////////////////////////////////////
                
                sceneEngine.GraphicsDevice.SetRenderTarget(RenderTarget3);
                RenderTarget3.GraphicsDevice.Clear(Color.DarkSlateBlue);
                sceneEngine.SpriteBatch.Begin();
                {
                    sceneEngine.SpriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, RtViewPort.Width, RtViewPort.Height), new Rectangle(RtViewPort.X, RtViewPort.Y, RtViewPort.Width, RtViewPort.Height), Color.White);
                }
                sceneEngine.SpriteBatch.End();
                
                stadia.Draw(birdEyeCamera, Matrix.Identity);
                Fielder1.Draw(birdEyeCamera, Matrix.Identity);
                if(KBRedBall != null)
                    KBRedBall.Draw(birdEyeCamera, Matrix.Identity);

                DisplayRemainingPhotoFrames();
                
                sceneEngine.GraphicsDevice.SetRenderTarget(null);

                RtViewPortTexture2 = RenderTarget3;
            }

            sceneEngine.GraphicsDevice.Viewport = NormalViewPort;
            sceneEngine.SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque);
            sceneEngine.SpriteBatch.Draw(OriginalViewPortTexture, new Vector2(0f, 0f), Color.White);

            sceneEngine.GraphicsDevice.Viewport = RtViewPort;

            if (bMobileAppear && bShowField)
            {
                //make mobile appear
                float fadeBetweenScenes = ((float)Math.Sin(MathHelper.ToRadians(TransitionValue)));

                PostEffect.Parameters["fFadeAmount"].SetValue(fadeBetweenScenes);
                PostEffect.Parameters["fSmoothSize"].SetValue(0.05f);
                PostEffect.Parameters["ColorTexture"].SetValue(RtViewPortTexture2);

                PostEffect.CurrentTechnique = PostEffect.Techniques[1];
                PostEffect.CurrentTechnique.Passes[0].Apply();
                {
                    sceneEngine.SpriteBatch.Draw(RtViewPortTexture1, new Rectangle(0, 0, NormalViewPort.Width, NormalViewPort.Height), Color.White);
                }
                if (fadeBetweenScenes >= 0.95f)
                {
                    bShowMobile = true;
                    bMobileDissappear = false;
                    bMobileAppear = false;
                    bShowField = false;
                    TransitionValue = 0.0f;
                }
            }
            else if (bMobileDissappear && bShowField)
            {
                // Apply the post process shader
                float fadeBetweenScenes = ((float)Math.Sin(MathHelper.ToRadians(TransitionValue)));

                PostEffect.Parameters["fFadeAmount"].SetValue(fadeBetweenScenes);
                PostEffect.Parameters["fSmoothSize"].SetValue(0.05f);
                PostEffect.Parameters["ColorTexture"].SetValue(RtViewPortTexture1);

                PostEffect.CurrentTechnique = PostEffect.Techniques[1];
                PostEffect.CurrentTechnique.Passes[0].Apply();
                {
                    sceneEngine.SpriteBatch.Draw(RtViewPortTexture2, new Rectangle(0, 0, NormalViewPort.Width, NormalViewPort.Height), Color.White);
                }
                if (fadeBetweenScenes >= 0.95f)
                {
                    bShowMobile = false;
                    bMobileDissappear = false;
                    bMobileAppear = false;
                    bShowField = true;
                    TransitionValue = 0.0f;
                }
            }
            else if (bShowMobile)
            {
                sceneEngine.SpriteBatch.Draw(RtViewPortTexture1, new Rectangle(0, 0, NormalViewPort.Width, NormalViewPort.Height), Color.White);
            }
            else if (bShowField)
            {
                sceneEngine.SpriteBatch.Draw(RtViewPortTexture2, new Rectangle(0, 0, NormalViewPort.Width, NormalViewPort.Height), Color.White);
            }
            sceneEngine.SpriteBatch.End();
            sceneEngine.GraphicsDevice.Viewport = NormalViewPort;

            // If the game is transitioning on or off, fade it out to black.
            if (TransitionPosition > 0 || pauseAlpha > 0)
            {
                float alpha = MathHelper.Lerp(1f - TransitionAlpha, 1f, pauseAlpha / 2);

                sceneEngine.FadeBackBufferToBlack(alpha);
            }

        }

    }
}
